package pseudoAssemblerCompiler.twopassassembler.view.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Created by IntelliJ IDEA.
 * User: Роман
 * Date: 21.03.12
 * Time: 12:22
 * To change this template use File | Settings | File Templates.
 */
public class AttributeEditorComboLine {

    private Composite _composite;

    private String _labelName;

    private Label _label;

    /**
     * Выпадающий список
     */
    private SmartCombo _comboViewer;

    /**
     * Текстовое поле для ввода данных
     */
    private Text _text;

    private boolean _isComboViewer;

    private boolean _isText;

    public AttributeEditorComboLine(Composite aComposite, String aLabelName) {
        _composite = new Composite(aComposite, SWT.NONE);
        _labelName = aLabelName;
    }

    public void createComboLine(int aLabelStyle, int aComboStyle) {
        createLabel(aLabelStyle);
        _comboViewer = WidgetsFactory.createSmartCombo(_composite, aComboStyle);
        _isComboViewer = true;
    }

    public void createText(int aLabelStyle, int aTextStyle) {
        createLabel(aLabelStyle);
        _text = WidgetsFactory.createText(_composite, aTextStyle);
        _isText = true;
    }

    private void createLabel(int aLabelStyle) {
        _label = WidgetsFactory.createLabel(_composite, aLabelStyle);
        _label.setText(_labelName);
    }

    private void layouts() {
        FormData formData;

        //Метка для выпадающего списка
        formData = new FormData();
        formData.left = new FormAttachment(0, 0);
        formData.top = new FormAttachment(0, 0);
        //formData.right = new FormAttachment(100, 0);
        //formData.bottom = new FormAttachment(100, 0);
        formData.width = 200;
        _label.setLayoutData(formData);

        //Выпадающий список
        formData = new FormData();
        formData.left = new FormAttachment(_label, 0);
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, -20);
        //formData.bottom = new FormAttachment(100, 0);
        formData.width = 200;
        if (_isComboViewer) {
            _comboViewer.getControl().setLayoutData(formData);
        }

        if (_isText) {
            _text.setLayoutData(formData);
        }
    }

    public void setLayout(FormLayout aLayout) {
        _composite.setLayout(aLayout);
    }

    public void setLayoutData(FormData aFormData) {
        _composite.setLayoutData(aFormData);
        layouts();
    }

    public Label getLabel() {
        return _label;
    }

    public SmartCombo getComboViewer() {
        return _comboViewer;
    }

    public Text getTextField() {
        return _text;
    }

}
